﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class recuperarTemplate : Form
    {
        public Form1 frm1;

        #region Function Keys
        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    this.Close();
                    break;

                case Keys.F1:
                    String conf = MessageBox.Show("Do you want to clear the templates history?", "Templates History", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
                    if (conf == "Yes")
                    {
                        deleteTemplatesHistory();
                        MessageBox.Show("Templates history cleared!", "Templates History", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    this.Close();
                    break;
            }

            return base.ProcessDialogKey(keyData);
        }
        #endregion

        #region Borderless Window
        protected override void WndProc(ref Message m)
        {
            // Let the base class have first crack
            base.WndProc(ref m);
            int WM_NCHITTEST = 0x84; // winuser.h
            // If the user clicked on the client area,
            // ask the OS to treat it as a click on the caption
            if (m.Msg != WM_NCHITTEST) return;
            int HTCLIENT = 1;
            int HTCAPTION = 2;
            if (m.Result.ToInt32() == HTCLIENT)
                m.Result = (IntPtr)HTCAPTION;
        }
        #endregion

        private settings set = new settings();

        public recuperarTemplate()
        {
            InitializeComponent();
        }

        private void deleteTemplatesHistory()
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(set["local_path"].ToString() + set["templates_folder"].ToString());
                foreach (FileInfo file in dir.GetFiles())
                {
                    file.Delete();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void recuperarTemplate_Load(object sender, EventArgs e)
        {
            dGrid.Columns.Clear();

            int n = 0;
            dGrid.Columns.Add("TITLE", "TITLE");

            try
            {
                DirectoryInfo Dir = new DirectoryInfo(set["local_path"].ToString() + set["templates_folder"].ToString());                

                List<FileInfo> FileList = Dir.GetFiles("*.txt", SearchOption.AllDirectories).ToList();
                FileList.Sort((b, a) => string.Compare(a.Name, b.Name)); 


                foreach (FileInfo FI in FileList)
                {
                    n = dGrid.Rows.Add();
                    string[] name = FI.Name.Split('.');
                    dGrid.Rows[n].Cells["TITLE"].Value = name[0];
                    n++;
                }

                if (n == 0) { MessageBox.Show("There are no templates in the history!", "Templates History", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dGrid_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                String tmp = dGrid.CurrentRow.Cells[0].Value.ToString();
                try
                {
                    TextReader tr = new StreamReader(set["local_path"].ToString() + set["templates_folder"].ToString() + tmp + ".txt");
                    Clipboard.SetText(tr.ReadToEnd());
                    tr.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    MessageBox.Show("Template loaded to clipboard!","Templates History", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    frm1.WindowState = FormWindowState.Minimized;
                    frm1.resetTodo();
                    this.Close();
                }
            }
        }
    }
}
